﻿using System;
using System.Drawing.Printing;
using System.Linq;
using System.Windows.Forms;

namespace BarcodePrint
{
    public partial class frmSetupPrinter : Form
    {
        public frmSetupPrinter()
        {
            InitializeComponent();
        }

        //  <ClientConfig>
        //  <ClientVersion>
        //  </ClientVersion>
        //  <PrintMethod>1</PrintMethod>
        //  <PrintParam>1</PrintParam>
        //  <PrintDarkness>25</PrintDarkness>
        //  <PrinterProgramLanguage>0</PrinterProgramLanguage>
        //</ClientConfig>

        public DeviceType deviceType = DeviceType.DRV;
        public string printParam = "1";
        private readonly PrinterProgramLanguage printerProgramLanguage = PrinterProgramLanguage.ZPL;

        private void frmSetupPrinter_Load(object sender, EventArgs e)
        {
            this.cmbPrinter.DataSource = PrinterSettings.InstalledPrinters.Cast<string>().ToList();
            this.cbPrinterProgramLanguage.DataSource = EnumUtil.GetEnumToList(typeof(PrinterProgramLanguage));
            this.cbPrinterProgramLanguage.DisplayMember = "Text";
            this.cbPrinterProgramLanguage.ValueMember = "Value";

            this.lblDarkness.Text = "25";
            this.hsbDarkness.Value = 25;
            this.cbPrinterProgramLanguage.SelectedValue = "0";

            this.radPrinter.Checked = this.cmbPrinter.Enabled = true;

            //if (deviceType == DeviceType.LPT)
            //{
            //    this.radLPT.Checked = this.tbLPTPort.Enabled = this.gbLPT.Enabled = true;
            //}
            //else if (deviceType == DeviceType.COM)
            //{
            //    this.radCOM.Checked = this.tbCOMPort.Enabled = true;
            //}
            //else if (deviceType == DeviceType.DRV)
            //{
            //    this.radPrinter.Checked = this.cmbPrinter.Enabled = true;
            //}
            //else if (deviceType == DeviceType.TCP)
            //{
            //    string[] values = CurrentPrintParam.Split(':');
            //    this.radTCP.Checked = this.tbRemoteIPAddress.Enabled = tbRemotePort.Enabled = true;
            //    this.tbRemoteIPAddress.Text = values[0];
            //    this.tbRemotePort.Text = values[1];
            //}
        }


        private void btnSubmit_Click(object sender, EventArgs e)
        {


            string darkness = this.lblDarkness.Text;
            string printerProgramlanguage = this.cbPrinterProgramLanguage.SelectedValue.ToString();

            if (this.radLPT.Checked)
            {
                deviceType = DeviceType.LPT;
                printParam = this.tbLPTPort.Text;
            }
            else if (this.radCOM.Checked)
            {
                deviceType = DeviceType.COM;
                printParam = this.tbCOMPort.Text;
            }
            else if (this.radPrinter.Checked)
            {
                deviceType = DeviceType.DRV;
                printParam = this.cmbPrinter.Text;
            }
            else if (this.radTCP.Checked)
            {
                deviceType = DeviceType.TCP;
                printParam = this.tbRemoteIPAddress.Text + ":" + this.tbRemotePort.Text;
            }

            if (printerProgramLanguage == PrinterProgramLanguage.ZPL)
            {
                try
                {
                    string cmd = string.Format("~SD{0}", darkness);
                    PrintUtil.Print(cmd, deviceType, printParam);
                    this.DialogResult = DialogResult.OK;
                }
                catch (Exception ex)
                {
                    MessageUtil.ShowError(ex.Message);
                }
            }
            else
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void btnIdentifySensor_Click(object sender, EventArgs e)
        {
            try
            {
                PrintUtil.Print("~JC", deviceType, printParam);
            }
            catch (Exception ex)
            {
                MessageUtil.ShowError(ex.Message);
            }
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            try
            {
                string cmd = @"^XA^PW1500^FO0,50^GB1500,40,20^FS^XZ";
                PrintUtil.Print(cmd, deviceType, printParam);
            }
            catch (Exception ex)
            {
                MessageUtil.ShowError(ex.Message);
            }
        }

        private void rad_CheckedChanged(object sender, EventArgs e)
        {
            Action<bool, bool, bool, bool> SetEnabled = (b, b1, b2, b3) =>
            {
                this.tbLPTPort.Enabled = b;
                this.tbCOMPort.Enabled = b1;
                this.cmbPrinter.Enabled = b2;
                this.tbRemoteIPAddress.Enabled = this.tbRemotePort.Enabled = b3;
                this.gbLPT.Enabled = b || b3;
            };

            RadioButton rb = sender as RadioButton;
            if (rb.Name == "radLPT")
            {
                SetEnabled.Invoke(true, false, false, false);
            }
            else if (rb.Name == "radCOM")
            {
                SetEnabled.Invoke(false, true, false, false);
            }
            else if (rb.Name == "radPrinter")
            {
                SetEnabled.Invoke(false, false, true, false);
            }
            else if (rb.Name == "radTCP")
            {
                SetEnabled.Invoke(false, false, false, true);
            }
            if (this.cbPrinterProgramLanguage.SelectedValue != null)
            {
                PrinterProgramLanguage language = (PrinterProgramLanguage)(int.Parse(this.cbPrinterProgramLanguage.SelectedValue.ToString()));
                this.btnTest.Enabled = this.btnIdentifySensor.Enabled = this.hsbDarkness.Enabled = language == PrinterProgramLanguage.ZPL;
            }
        }

        private void hsbDarkness_Scroll(object sender, ScrollEventArgs e)
        {
            this.lblDarkness.Text = e.NewValue.ToString();
        }

    }
}
